//
//  SLAuthenticationPush.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/28.
//  
  

#import "SLAuthenticationPush.h"
#import "OriginData.h"
#import <YYKit/YYKit.h>

@implementation SLAuthenticationPush

// 跳转开始认证页面
+ (void)pushStartAuthenticationPageWithDict:(NSDictionary *)dict view:(UIView *)view {
    if (dict) {
        NSString *productID = dict[@"swelling"][@"didn"]; //产品id
        NSDictionary *applied = dict[@"applied"];
        NSString *remove = applied[@"remove"]; //跳转的下一步的类型（用于判断具体到了哪一步）
        if (remove.length == 0) {
            // 已完成
            Analysis.loanStart.start = NSDate.date;
            NSString *orderId = dict[@"swelling"][@"broad"];
            [self requestProductFollowWithOrderID:orderId view:view completion:^(NSDictionary *dict) {
                Analysis.loanStart.end = NSDate.date;
                [SLLocationService startLocationWithCompletion:^(CGFloat lat, CGFloat lon) {
                    [Analysis sendDataWithProductID:productID time:Analysis.loanStart];
                }];
                NSString *elegance = dict[@"elegance"];
                [SLRoutePush route:elegance parameter:nil];
            }];
        } else {
            NSArray *favorablys = dict[@"favorably"];
            NSDictionary *dict = nil;
            for (NSDictionary *kv in favorablys) {
                if ([kv[@"contained"] sl_intValue] == 0) {
                    dict = kv;
                    break;
                }
            }
            NSDictionary *parameter = @{
                @"productId": productID ?: @"",
                @"stepCount": @(favorablys.count),
                @"currentStep": @([favorablys indexOfObject:dict]+1),
            };
            [self routeWithPageId:remove parameter:parameter];
        }
    }
}

// 跳转到对应的认证项
+ (void)pushAuthenticationItemPageWithDict:(NSDictionary *)dict index:(NSInteger)index view:(UIView *)view {
    NSString *productId = dict[@"swelling"][@"didn"];
    NSArray *favorablys = dict[@"favorably"];
    NSDictionary *favorably = favorablys[index];
    NSString *remove = favorably[@"remove"]; //跳转的下一步的类型（用于判断具体到了哪一步）
    NSString *contained = favorably[@"contained"]; //是否已完成认证 0否1是
    if (remove.length > 0 && [contained sl_intValue] == 1) {
        NSDictionary *parameter = @{
            @"productId": productId ?: @"",
            @"stepCount": @(favorablys.count),
            @"currentStep": @(index+1),
        };
        [self routeWithPageId:remove parameter:parameter];
    } else {
        [self pushStartAuthenticationPageWithDict:dict view:view];
    }
}

+ (void)pushNextPageWithProductID:(NSString *)productId view:(UIView *)view {
    [self requestProductDetailWithProductID:productId view:view completion:^(NSDictionary *dict) {
        [self pushStartAuthenticationPageWithDict:dict view:view];
    }];
}

+ (void)requestProductDetailWithProductID:(NSString *)productId view:(UIView *)view completion:(void(^)(NSDictionary *dict))completion {
    [MBProgressHUD showHUDAddedTo:view animated:YES];
    [ProductDetailRequestProvider requestProductDetailWithProductID:productId success:^(NSDictionary * _Nonnull dict) {
        [MBProgressHUD hideHUDForView:view animated:YES];
        !completion ?: completion(dict);
    } failure:^(YTKRequest * _Nonnull request) {
        !completion ?: completion(nil);
        [MBProgressHUD hideHUDForView:view animated:YES];
        [ResponseToast showToastWithRequest:request];
    }];
}
+ (void)requestProductFollowWithOrderID:(NSString *)orderId view:(UIView *)view completion:(void(^)(NSDictionary *dict))completion {
    [MBProgressHUD showHUDAddedTo:view animated:YES];
    [OrderFollowRequestProvider requestOrderFollowWithOrderId:orderId success:^(NSDictionary * _Nonnull dict) {
        [MBProgressHUD hideHUDForView:view animated:YES];
        !completion ?: completion(dict);
    } failure:^(YTKRequest * _Nonnull request) {
        !completion ?: completion(nil);
        [MBProgressHUD hideHUDForView:view animated:YES];
        [ResponseToast showToastWithRequest:request];
    }];
}
+ (void)routeWithPageId:(NSString *)remove parameter:(NSDictionary *)parameter {
    NSString *pageId = [OriginData removeOrigin:remove];
    if ([pageId isEqualToString:@"public"]) {
        [SLRoutePush route:@"SLIdentityVerificationViewController" parameter:parameter];
    } else if ([pageId isEqualToString:@"personal"]) {
        [SLRoutePush route:@"SLBasicInformationViewController" parameter:parameter];
    } else if ([pageId isEqualToString:@"job"] || [pageId isEqualToString:@"work"]) {
        [SLRoutePush route:@"SLProofOfSolvencyViewController" parameter:parameter];
    } else if ([pageId isEqualToString:@"ext"]) {
        [SLRoutePush route:@"SLContactMechanismViewController" parameter:parameter];
    } else if ([pageId isEqualToString:@"bank"]) {
        [SLRoutePush route:@"SLRepaymentViewController" parameter:parameter];
    }
}


/// 退到产品详情页
+ (void)popToProductDetailViewController:(UIViewController *)currentCtrl {
    NSArray<UIViewController *> *ctrls = currentCtrl.navigationController.viewControllers;
    UIViewController *detailCtrl = nil;
    for (UIViewController *ctrl in ctrls) {
        if ([ctrl isKindOfClass:NSClassFromString(@"SLCoreCertificationViewController")]) {
            detailCtrl = ctrl;
            break;
        }
    }
    [currentCtrl.navigationController popToViewController:detailCtrl animated:YES];
}

@end
